<?php

/**
 * @author aisyah
 * @copyright 2012
 */
 
include "user.php";


class User_Dao{
    
    var $user = array();
    var $user_id;
    var $query;
    var $error;
    var $db;
    
    public function __construct(Database $db){
        $this->db = $db;
    }
    
    public function add_user(User $user){
	
			$this->db->connect->connect();
	
			if($this->table_Exists('user')){
	
				$this->query = "
								INSERT
								INTO
								user
								(
									user_name,
									password,
									category,
									last_login
	
								)
								VALUES
								(
									'".$user->user_name."',
									'".$user->password."',
									'".$user->category."',
									'".$user->last_login."',
								)
								";
				mysql_query($this->query);
				$this->db->connect->disconnect();
	
			}else{
	
	            $this->error = 'data tidak ditemukan';
	            return $this->error;
	
	        }

		}   
	
	public function get_user($id=null,$posisi=null, $batas=null){
        
        $this->db->connect->connect();
        
        if($this->db->table_Exists('user')){
            
            if($id!=null AND $posisi==null AND $batas==null){
                $this->query = "SELECT
								*
								FROM
								user
								WHERE 
								user_id = '".$id."'
								";
            }else if($id!=null AND !is_null($posisi) AND !is_null($batas==null)){
                $this->query = "SELECT
								*
								FROM
								user
								WHERE 
								user_id = '".$id."'
								LIMIT '".$posisi."','".$batas
								;
            }else if(!is_null($posisi) AND !is_null($batas==null)){
                $this->query = "SELECT
								*
								FROM
								user               
								LIMIT $posisi,$batas";
            }else{
                $this->query = "SELECT
								*
								FROM
								user      
								";
            }
            
            
            $result = mysql_query($this->query);                 
            
            if($result){
                
                while($r=mysql_fetch_array($result)){
                    
                    $user = new User();
                    
                    $user->user_id = $r['user_id'];
                    $user->username = $r['username'];
                    $user->password = $r['password'];
                    $user->category = $r['category'];
                    $user->last_login = $r['last_login'];
                    
                    
                    $this->user[] = $user;
                    
                    
                }
                
                $this->db->connect->disconnect();
            
                return $this->user;
            }else{
                
                $this->error = 'data tidak ditemukan';
                return $this->error;
                
            }      
            
        }else{
            
            $this->error = 'data tidak ditemukan';
            return $this->error;
            
        }             
        
    }
    
        
        
    } 